// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
let base = require("panelBase")
let viewConfig = {
    btn_back:{
        node:null,
        name:"con_bg/btn_close",
        eventType:"click",
        cb:"onBtnCloseClick",
    },
    tog_task:{
        node:null,
        name:"con_bg/tog/task",
        eventType:"toggle",
        cb:"onTaskClick",
    },
    tog_achieve:{
        node:null,
        name:"con_bg/tog/achieve",
        eventType:"toggle",
        cb:"onAchieveClick"
    },


    //分红蛋糕
    fh_Name:{
        node:null,
        name:"con_bg/buttom/fh"
    },
    fh_des:{
        node:null,
        name:"con_bg/buttom/fh_des"
    },
    fh_date:{
        node:null,
        name:'con_bg/buttom/fh_date'
    },
    btn_fh_info:{
        node:null,
        name:"con_bg/buttom/btn_info",
        eventType:"click",
        cb:"onBtnInfoClick"
    },
    btn_fh_buy:{
        node:null,
        name:"con_bg/buttom/btn_fh",
        eventType:"click",
        cb:"onBtnBuyClick"
    },
    fh_cake_price:{
        node:null,
        name:"con_bg/buttom/btn_fh/Label"
    },
    taskItem:{
        node:null,
        name:"con_bg/taskItem",
        active:false,
    },
    sc:{
        node:null,
        name:"con_bg/Sc"
    }
}
cc.Class({
    extends: base,

    properties: {
        mySc:null,
        taskBgList:{
            default:[],
            type:cc.SpriteFrame,
        },
        taskbtnBgList:{
            default:[],
            type:cc.SpriteFrame,
        },
        status:[],
        yellow:null,
    },
    init(){
        this._super(viewConfig);
        this.status = ["未完成","已完成"]
        this.node.addComponent("myScrollView");
        this.mySc = this.node.getComponent("myScrollView");
        this.mySc.init(viewConfig["sc"].node);
        this.yellow = new cc.Color(171,126,64,255);
        this.isInited = true;
        this.onHide();
    },

    onShow(){
        this._super();

        // setTimeout(()=>{
        //     let testList = [];
        //    for(let i = 0 ; i < 6; ++i )
        //    {
        //        let testData = {
        //            des: i %2 == 0 ? "每日登陆" : "充值999万",
        //            reward: 1000 + i*500,
        //            progress:0.5,
        //            status: i %2 == 0 ? 0 : 1, // 0 未完成 ,1 完成
        //        }
        //       testList.push(testData);
        //    }
        //    this.mySc.showData(viewConfig["taskItem"].node,testList,this.initTaskItem.bind(this),10)
        // },1000);


        //测试广告
      //  cc.tg.adDataCenter.toShowAd("task_ex");
       // cc.tg.adDataCenter.toShowAd("task_re");
    },  

    initTaskItem(node,data){
        if(!node || !data)
            return ;
        this.updateInfo(node.getChildByName("task_title"),data.des,cc.Label);
        this.updateInfo(node.getChildByName("reward"),data.reward,cc.Label);
        let pb = node.getChildByName("task_pb");
        pb.getComponent(cc.ProgressBar).progress = data.progress;
        this.updateInfo(node.getChildByName("bg"),this.taskBgList[data.status],cc.Sprite);
        this.updateInfo(node.getChildByName("task_btn"),this.taskbtnBgList[data.status],cc.Sprite);
        let label = cc.find("task_btn/Label",node);
        this.updateInfo(label,this.status[data.status],cc.Label);
        
    },


    onBtnTaskItemClick(e){
        let index = e.target.parent.index;
        cc.log(index);
        this.changeColor(e.target,new cc.Color(255,255,255));
        this.changeColor(viewConfig["tog_achieve"].node,this.yellow);
    },
    onBtnCloseClick(){
        this.onHide();
    },
    onTaskClick(){
        this.changeColor(e.target,new cc.Color(255,255,255));
        this.changeColor(viewConfig["tog_task"].node,this.yellow);
    },
    onAchieveClick(){
        return ;
    },

    onBtnInfoClick(){
        cc.log(" on click  info ");
    },
    onBtnBuyClick(){
        cc.log(" on click  buy ");

    },

    onDestroy(){
        for(let key in viewConfig)
        {
            viewConfig[key].node = null;
        }
    },

    changeColor(btn,color)
    {
        if(!btn || !color )
            return ;
        let label = btn.children[0];
        label.color = color;
    }

});
